package com.enzoism.springboot.dive.scene1.simple;

import com.enzoism.springboot.dive.common.AppConstants;
import com.enzoism.springboot.dive.common.CompareSortUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class Scene1SimpleTest {
    /**
     * 场景一：单字段排序【简写】（根据前端传递的字段进行排序）
     * Map传递格式：sortMap:{"sortField":"usrId","sortType":"desc"}
     */
    public static void main(String[] args) {
        List<Scene1SimpleModel> scene1Models = Scene1SimpleTest.initDataList();

        Map<String, String> sortMap = new HashMap<>();
        sortMap.put(AppConstants.ORDER_FIELD, AppConstants.ORDER_USR_NUM);
        sortMap.put(AppConstants.ORDER_TYPE, AppConstants.ORDER_TYPE_DESC);
        // 进行排序简写
        Collections.sort(scene1Models, new Comparator<Scene1SimpleModel>() {
            @Override
            public int compare(Scene1SimpleModel o1, Scene1SimpleModel o2) {
                // sortMap直接从上面的参数中传递过
                if (MapUtils.isNotEmpty(sortMap)) {
                    String sortField = sortMap.get(AppConstants.ORDER_FIELD);
                    String sortType = sortMap.get(AppConstants.ORDER_TYPE);
                    int sortIntValue = CompareSortUtil.getSortIntValue(sortType);

                    if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_CLK_NUM) &&
                            o1.getClkNum() != o2.getClkNum()) {
                        return sortIntValue * o1.getClkNum().compareTo(o2.getClkNum());
                    } else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_NUM) &&
                            o1.getUsrNum() != o2.getUsrNum()) {
                        return sortIntValue * o1.getUsrNum().compareTo(o2.getUsrNum());
                    } else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_ID) &&
                            o1.getUsrId() != o2.getUsrId()) {
                        return sortIntValue * o1.getUsrId().compareTo(o2.getUsrId());
                    }
                }
                return o1.getUsrId().compareTo(o2.getUsrId());
            }
        });
        CompareSortUtil.printDataList(scene1Models);
    }


    public static List<Scene1SimpleModel> initDataList() {
        List<Scene1SimpleModel> dataList = new ArrayList<>();
        Scene1SimpleModel scene1Model1 = new Scene1SimpleModel("001", 010, 010);
        Scene1SimpleModel scene1Model2 = new Scene1SimpleModel("001", 010, 001);
        Scene1SimpleModel scene1Model3 = new Scene1SimpleModel("001", 101, 300);
        Scene1SimpleModel scene1Model4 = new Scene1SimpleModel("002", 31, 178);
        Scene1SimpleModel scene1Model5 = new Scene1SimpleModel("003", 51, 881);
        Scene1SimpleModel scene1Model6 = new Scene1SimpleModel("004", 61, 1);
        Scene1SimpleModel scene1Model7 = new Scene1SimpleModel("009", 178, 10);
        Scene1SimpleModel scene1Model8 = new Scene1SimpleModel("005", 13, 129);
        Scene1SimpleModel scene1Model9 = new Scene1SimpleModel("006", 100, 150);
        dataList.add(scene1Model1);
        dataList.add(scene1Model2);
        dataList.add(scene1Model3);
        dataList.add(scene1Model4);
        dataList.add(scene1Model5);
        dataList.add(scene1Model6);
        dataList.add(scene1Model7);
        dataList.add(scene1Model8);
        dataList.add(scene1Model9);
        return dataList;
    }
}
